import { defineStore } from "pinia";
import piniaPersistConfig from "@/stores/helper/persist";
import { getOrganicTree } from "@/api/modules/organize";
import { getAllRoleList } from '@/api/basic/role';
import { convertToTreeSelectData } from '@/utils/common'

export const useDataStore = defineStore({
  id: "geeker-data",
  state: (): any => ({
    departmentList: [] as any[],
    deptList: [] as any[],
    roleList: []
  }),
  getters: {},
  actions: {
    // 获取部门列表
    async fetchDepartmentList() {
      try {
        const res = await getOrganicTree();
        if (!res) {
          return;
        } else {
          this.deptList = res
          this.departmentList = convertToTreeSelectData(res);
        }
      } catch (error) {
        console.error("请求失败:", error);
      }
    },
    // 获取角色列表
    async fetchRoleList() {
      try {
        const res = await getAllRoleList();
        this.roleList = res;
      } catch (error) {
        console.error("请求失败:", error);
      }
    }
  },
  persist: piniaPersistConfig("geeker-data")
});
